﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// add
using System.Diagnostics;
using System.IO;


namespace FormBaseLib
{
    public class CAction
    {
        StringBuilder msb = new StringBuilder();
        //DateTime dBegin = new DateTime();
        //DateTime dEnd = new DateTime();
        //string msMessage;
        public int FormatLinePrefix { get; set; }
        //public string Result { get; set; }
        //public string LastText { get; private set; }
        public string msActionName { get; private set; }
        public string msMessage { get; private set; }
        public bool mbDebug { get; set; } // 預設開啟, 預設讀取設定後關閉.
        public string msMessageLog { get { return msb.ToString(); } }
        public DateTime mBeginTime { get; private set; }
        public DateTime mEndTime { get; private set; }
        public string msFilename { get; set; }
        public int miProgressValue { get; set; }

        public CAction()
        {
            Clear();
            FormatLinePrefix = 1;
        }
        public void Clear()
        {
            msMessage = string.Empty;
            msb.Length = 0;
            mBeginTime = DateTime.Now;
            mEndTime = mBeginTime;

        }
        //public void ClearMsg()
        //{
        //    msMessage = string.Empty;
        //    dEnd = DateTime.Now;
        //    msb.Length = 0;
        //}
        public void AppendMsg(string sMsg)
        {
            Debug.WriteLine(sMsg);
            msMessage = sMsg;
            mEndTime = DateTime.Now;
            msb.AppendLine(string.Format("{0},MSG,{1}.", PrefixEndTime(), sMsg));
        }
        public void AppendMsgError(string sMsg)
        {
            Debug.WriteLine(sMsg);
            msMessage = sMsg;
            mEndTime = DateTime.Now;
            msb.AppendLine(string.Format("{0},ERROR,{1}.", PrefixEndTime(), sMsg));
        }
        public void AppendMsgDebug(string sMsg)
        {
            Debug.WriteLine(sMsg);
            if (mbDebug)
            {
                msMessage = sMsg;
                mEndTime = DateTime.Now;
                msb.AppendLine(string.Format("{0},DEBUG,{1}.", PrefixEndTime(), sMsg));
            }
        }
        public void EndError(string sMsg)
        {
            AppendMsgError(sMsg);
        }
        public void EndSuccess(string sMsg)
        {
            msMessage = sMsg;
            mEndTime = DateTime.Now;
            msb.AppendLine(string.Format("{0},SUCCESS,{1}.", PrefixEndTime(), sMsg));
        }
        private string PrefixEndTime()
        {
            switch (FormatLinePrefix)
            {
                case 0:
                    return string.Empty;
                case 2:
                    return mEndTime.ToString("HH:mm:ss.fff");
                case 3:
                    return mEndTime.ToString("HH:mm");
                default:  // = 1
                    return mEndTime.ToString("HH:mm:ss");
            }
        }
        public void BeginAction(string sActionName)
        {
            Clear();
            msActionName = sActionName;
            msb.AppendLine(string.Format("{0},BEGIN,{1},{2}.", PrefixEndTime(), sActionName, mEndTime.ToString("yyyy-MM-dd HH:mm:ss.fff")));
        }
        public void SaveLog()
        {
            mEndTime = DateTime.Now;
            if (string.IsNullOrEmpty(msFilename))
                msFilename = @".\ActionLog-" + mEndTime.Second.ToString("00") + ".txt";

            File.WriteAllText(msFilename, msMessageLog);
        }
        public void OpenWithNotePad()
        {
        }
    }
}
